{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# face++平台面部检测："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"-fVzoDx0MDuAF0p24MOY5_xr0j2CeB1X\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'jx_yssCi5SfGJVFsehy-7GOSO_hDs8_S'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://c-ssl.duitang.com/uploads/item/201903/17/20190317091828_LKTzM.thumb.700_0.jpeg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603388227,9e821512-7ace-427b-b1e5-e59d224a09f7\",\"time_used\":1580,\"faces\":[{\"face_token\":\"623115973a9c3985df3190b517f6ccea\",\"face_rectangle\":{\"top\":283,\"left\":153,\"width\":304,\"height\":304},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":20},\"smile\":{\"value\":0.077,\"threshold\":50.000},\"emotion\":{\"anger\":1.459,\"disgust\":0.030,\"fear\":0.030,\"happiness\":0.030,\"neutral\":81.116,\"sadness\":17.304,\"surprise\":0.030}}}],\"image_id\":\"HFx3L2A2LStL+O1L0ByizQ==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603388227,9e821512-7ace-427b-b1e5-e59d224a09f7\",\"time_used\":1580,\"faces\":[{\"face_token\":\"623115973a9c3985df3190b517f6ccea\",\"face_rectangle\":{\"top\":283,\"left\":153,\"width\":304,\"height\":304},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":20},\"smile\":{\"value\":0.077,\"threshold\":50.000},\"emotion\":{\"anger\":1.459,\"disgust\":0.030,\"fear\":0.030,\"happiness\":0.030,\"neutral\":81.116,\"sadness\":17.304,\"surprise\":0.030}}}],\"image_id\":\"HFx3L2A2LStL+O1L0ByizQ==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603388227,9e821512-7ace-427b-b1e5-e59d224a09f7',\n",
       " 'time_used': 1580,\n",
       " 'faces': [{'face_token': '623115973a9c3985df3190b517f6ccea',\n",
       "   'face_rectangle': {'top': 283, 'left': 153, 'width': 304, 'height': 304},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.077, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.459,\n",
       "     'disgust': 0.03,\n",
       "     'fear': 0.03,\n",
       "     'happiness': 0.03,\n",
       "     'neutral': 81.116,\n",
       "     'sadness': 17.304,\n",
       "     'surprise': 0.03}}}],\n",
       " 'image_id': 'HFx3L2A2LStL+O1L0ByizQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603388227,9e821512-7ace-427b-b1e5-e59d224a09f7',\n",
       " 'time_used': 1580,\n",
       " 'faces': [{'face_token': '623115973a9c3985df3190b517f6ccea',\n",
       "   'face_rectangle': {'top': 283, 'left': 153, 'width': 304, 'height': 304},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.077, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.459,\n",
       "     'disgust': 0.03,\n",
       "     'fear': 0.03,\n",
       "     'happiness': 0.03,\n",
       "     'neutral': 81.116,\n",
       "     'sadness': 17.304,\n",
       "     'surprise': 0.03}}}],\n",
       " 'image_id': 'HFx3L2A2LStL+O1L0ByizQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简化面部检测后获得的人脸数据(以pandas为例)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# results['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.json_normalize(results,record_path='faces')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>623115973a9c3985df3190b517f6ccea</td>\n",
       "      <td>283</td>\n",
       "      <td>153</td>\n",
       "      <td>304</td>\n",
       "      <td>304</td>\n",
       "      <td>Male</td>\n",
       "      <td>20</td>\n",
       "      <td>0.077</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.459</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>81.116</td>\n",
       "      <td>17.304</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  623115973a9c3985df3190b517f6ccea                 283                  153   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   304                    304                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    20                   0.077                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     1.459                        0.03   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                     0.03                          0.03   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                      81.116                      17.304   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                         0.03  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>623115973a9c3985df3190b517f6ccea</td>\n",
       "      <td>283</td>\n",
       "      <td>153</td>\n",
       "      <td>304</td>\n",
       "      <td>304</td>\n",
       "      <td>Male</td>\n",
       "      <td>20</td>\n",
       "      <td>0.077</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.459</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>81.116</td>\n",
       "      <td>17.304</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  623115973a9c3985df3190b517f6ccea                 283                  153   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   304                    304                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    20                   0.077                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     1.459                        0.03   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                     0.03                          0.03   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                      81.116                      17.304   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                         0.03  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_face_rectangle = df[['face_token','face_rectangle.top','face_rectangle.left','face_rectangle.width','face_rectangle.height']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# face ++平台人脸集合："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 步骤：\n",
    "- api调用准备工作\n",
    "- 创建人脸集合\n",
    "- 获取人脸集合信息\n",
    "- 增加人脸信息\n",
    "- 移除人脸信息\n",
    "- 更新人脸信息\n",
    "- 对比人脸相似度：a.直接对比；b.与人脸集合对比（获取face_token）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# api 调用准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#api 调用准备工作\n",
    "api_secret = \"-fVzoDx0MDuAF0p24MOY5_xr0j2CeB1X\"\n",
    "api_key = \"jx_yssCi5SfGJVFsehy-7GOSO_hDs8_S\"  # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  1. FaceSet Create (创建人脸集合)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. FaceSet Create 创建人脸集合\n",
    "import requests,json\n",
    "\n",
    "display_name = \"十七集合\"\n",
    "outer_id = \"1910000\"\n",
    "global outer_id\n",
    "user_data = \"13人，都是野男人\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "    'display_name':display_name,\n",
    "    'user_data':user_data,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'time_used': 136,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603393010,63e9d126-6765-4aac-88e5-3c6cf81f4654',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'time_used': 136,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603393010,63e9d126-6765-4aac-88e5-3c6cf81f4654',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.FaceSet GetDetail (获取人脸集合信息)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'tags': '',\n",
       " 'time_used': 102,\n",
       " 'user_data': '13人，都是野男人',\n",
       " 'display_name': '十七集合',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603393046,8ddbee41-e7a9-4a0b-adbc-e5a2d4a00a46',\n",
       " 'outer_id': '1910000'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'tags': '',\n",
       " 'time_used': 102,\n",
       " 'user_data': '13人，都是野男人',\n",
       " 'display_name': '十七集合',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603393046,8ddbee41-e7a9-4a0b-adbc-e5a2d4a00a46',\n",
       " 'outer_id': '1910000'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 插入面部检测 获取facetoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = \"https://c-ssl.duitang.com/uploads/item/201903/17/20190317091828_LKTzM.thumb.700_0.jpeg\"\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603393664,39216423-dab9-4907-a788-e74f4aaa75ee',\n",
       " 'time_used': 219,\n",
       " 'faces': [{'face_token': 'a7c5e4dca90efd2b8a847dcc7b6c86e2',\n",
       "   'face_rectangle': {'top': 283, 'left': 153, 'width': 304, 'height': 304},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.077, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.459,\n",
       "     'disgust': 0.03,\n",
       "     'fear': 0.03,\n",
       "     'happiness': 0.03,\n",
       "     'neutral': 81.116,\n",
       "     'sadness': 17.304,\n",
       "     'surprise': 0.03}}}],\n",
       " 'image_id': 'HFx3L2A2LStL+O1L0ByizQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603393664,39216423-dab9-4907-a788-e74f4aaa75ee',\n",
       " 'time_used': 219,\n",
       " 'faces': [{'face_token': 'a7c5e4dca90efd2b8a847dcc7b6c86e2',\n",
       "   'face_rectangle': {'top': 283, 'left': 153, 'width': 304, 'height': 304},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.077, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.459,\n",
       "     'disgust': 0.03,\n",
       "     'fear': 0.03,\n",
       "     'happiness': 0.03,\n",
       "     'neutral': 81.116,\n",
       "     'sadness': 17.304,\n",
       "     'surprise': 0.03}}}],\n",
       " 'image_id': 'HFx3L2A2LStL+O1L0ByizQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.FaceSet AddFace (增加人脸信息)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'08d936ca3cd95d8657bf32143e209b71',\n",
    "    'face_tokens':'a7c5e4dca90efd2b8a847dcc7b6c86e2'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'time_used': 2865,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603393706,2906e222-3388-4471-9c94-e52d02409523',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'time_used': 2865,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603393706,2906e222-3388-4471-9c94-e52d02409523',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.FaceSet RemoveFace（移除人脸信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'08d936ca3cd95d8657bf32143e209b71',\n",
    "    'face_tokens':'a7c5e4dca90efd2b8a847dcc7b6c86e2',\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 184,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603393866,e9327eaa-9126-4422-bbd6-a39549ccec6e',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 184,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603393866,e9327eaa-9126-4422-bbd6-a39549ccec6e',\n",
       " 'outer_id': '1910000',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.FaceSet Update（更新人脸集合信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'08d936ca3cd95d8657bf32143e209b71',\n",
    "    'user_data':\"13人，都是野男人\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'request_id': '1603394015,b49e5811-249a-4a96-aac5-a880f08cb2d9',\n",
       " 'time_used': 73,\n",
       " 'outer_id': '1910000'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '08d936ca3cd95d8657bf32143e209b71',\n",
       " 'request_id': '1603394015,b49e5811-249a-4a96-aac5-a880f08cb2d9',\n",
       " 'time_used': 73,\n",
       " 'outer_id': '1910000'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. Compare Face（对比人脸相似度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "hongzhixiu01 = \"https://b-ssl.duitang.com/uploads/item/201807/31/20180731214616_LAUkc.thumb.700_0.jpeg\"\n",
    "hongzhixiu02 = \"https://p3.ssl.qhimgs1.com/sdr/400__/t01d0e35abed8ea4ef9.jpg\"\n",
    "wenjunhui = \"https://p2.ssl.qhimgs1.com/sdr/400__/t01524bb9525f7d0c1c.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ①直接对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':hongzhixiu01,\n",
    "    'image_url2':wenjunhui,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 256,\n",
       "    'top': 256,\n",
       "    'left': 227,\n",
       "    'height': 256},\n",
       "   'face_token': '2d27a4930aa24f3f8e4f9c2005f86744'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 101,\n",
       "    'top': 95,\n",
       "    'left': 72,\n",
       "    'height': 101},\n",
       "   'face_token': '7f4c53796fb184b3fb4d9d403f057c53'}],\n",
       " 'time_used': 2892,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 67.421,\n",
       " 'image_id2': 'ROM1bWL/zm7Hu4GIAOlQ/Q==',\n",
       " 'image_id1': 'Ndh0lRCVx1I9zTXN8u+eSg==',\n",
       " 'request_id': '1603394481,9a8b13b3-c4cc-4ddd-8b11-46c33815974d'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 256,\n",
       "    'top': 256,\n",
       "    'left': 227,\n",
       "    'height': 256},\n",
       "   'face_token': '2d27a4930aa24f3f8e4f9c2005f86744'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 101,\n",
       "    'top': 95,\n",
       "    'left': 72,\n",
       "    'height': 101},\n",
       "   'face_token': '7f4c53796fb184b3fb4d9d403f057c53'}],\n",
       " 'time_used': 2892,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 67.421,\n",
       " 'image_id2': 'ROM1bWL/zm7Hu4GIAOlQ/Q==',\n",
       " 'image_id1': 'Ndh0lRCVx1I9zTXN8u+eSg==',\n",
       " 'request_id': '1603394481,9a8b13b3-c4cc-4ddd-8b11-46c33815974d'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 方案2:与人脸集合进行对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = hongzhixiu01\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603394599,3d2e841f-b53e-4b18-97f4-0a98cf7e719b',\n",
       " 'time_used': 231,\n",
       " 'faces': [{'face_token': '64e435786f64373b395a5c804a390e32',\n",
       "   'face_rectangle': {'top': 256, 'left': 227, 'width': 256, 'height': 256},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 25.014, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.228,\n",
       "     'disgust': 0.041,\n",
       "     'fear': 0.041,\n",
       "     'happiness': 0.318,\n",
       "     'neutral': 75.367,\n",
       "     'sadness': 23.66,\n",
       "     'surprise': 0.347}}}],\n",
       " 'image_id': 'Ndh0lRCVx1I9zTXN8u+eSg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603394599,3d2e841f-b53e-4b18-97f4-0a98cf7e719b',\n",
       " 'time_used': 231,\n",
       " 'faces': [{'face_token': '64e435786f64373b395a5c804a390e32',\n",
       "   'face_rectangle': {'top': 256, 'left': 227, 'width': 256, 'height': 256},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 25.014, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.228,\n",
       "     'disgust': 0.041,\n",
       "     'fear': 0.041,\n",
       "     'happiness': 0.318,\n",
       "     'neutral': 75.367,\n",
       "     'sadness': 23.66,\n",
       "     'surprise': 0.347}}}],\n",
       " 'image_id': 'Ndh0lRCVx1I9zTXN8u+eSg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
